package question8;

public class Main {

    public String longestNiceSubstring(String s) {
        int posStart = 0;
        int maxLength = 0;
        for (int i = 0; i < s.length(); i++) {
            int lower = 0;
            int upper = 0;
            for (int j = i; j < s.length(); j++) {
                if (Character.isLowerCase(s.charAt(j))) {
                    lower |= 1 << (s.charAt(j) - 'a');
                }
                if (Character.isUpperCase(s.charAt(j))) {
                    upper |= 1 << (s.charAt(j) - 'A');
                }
                if (lower == upper && j - i + 1 > maxLength) {
                    posStart = i;
                    maxLength = j - i + 1;
                }
            }
        }
        return s.substring(posStart, posStart + maxLength);
    }

    public static void main(String[] args) {
        String s = new Main().longestNiceSubstring("YazaAay");
        System.out.println(s);
    }
}
